<script setup lang="ts">
import { onMounted, ref, computed } from "vue";

import { Icons } from "@/constants/images";
import { useExamStore } from "@/store/modules/exam";
const examStore = useExamStore();

const props = withDefaults(
    defineProps<{
        question: any; // 题目
        isright: boolean; // 是否正确
        isResult: boolean; // 是否是结果页
    }>(),
    {
        isright: false,
        isResult: false,
    }
);

const duration = ref("00:00");
const progressTime = ref(0);


const secondsToMinutes = (seconds: any) => {
    // 计算分钟数  
    let minutes = Math.floor(seconds / 60);
    // 计算剩余的秒数  
    let remainingSeconds = seconds % 60;

    // 使用字符串的padStart方法来确保分钟和秒都是两位数  
    // '02'表示如果字符串长度小于2，则在前面填充'0'直到长度为2  
    let formattedMinutes = minutes.toString().padStart(2, '0');
    let formattedSeconds = remainingSeconds.toString().padStart(2, '0');

    // 返回结果  
    return `${formattedMinutes}:${formattedSeconds}`;
}
onMounted(() => {
    const audioElement = document.getElementById('audioElement');

    // 当音频的元数据加载完成后（如时长、轨道数等），执行此函数  
    //@ts-ignore
    audioElement.addEventListener('loadedmetadata', function () {
        // 获取并显示音频时长
        //@ts-ignore
        var dura = Math.round(audioElement.duration);
        duration.value = secondsToMinutes(dura);
    });
    //@ts-ignore
    audioElement.addEventListener('timeupdate', () => {
        //@ts-ignore
        var currentTime = audioElement.currentTime, duration = audioElement.duration;
        progressTime.value = currentTime / duration * 100;
    });
})
const isMobile = computed(() => {
    const viewportWidth = document.documentElement.clientWidth;
    return viewportWidth <= 768;
});

const questionId = ref(0);

//@ts-ignore
const writeAnswer = (answer) => {
    examStore.setProgress();
}

const focus = (id: any) => {
    questionId.value = id;
}
</script>

<template>
    <div class="question">
        <div class="top-box">
            <div>
                <div class="title">{{ props.question.title }}</div>
                <div class="describe" v-html="props.question.describe"></div>
            </div>
            <div class="audio-box">
                <audio id="audioElement" :src="props.question.audioUrl" controls="false" autoplay hidden="true"></audio>
                <img class="icon_voice_play play" :src="Icons.ICON_PLAY">
                <progress v-if="!isMobile" class="progress is-primary is-small" :value="progressTime" max="100" />
                <span>{{ duration }}</span>
            </div>
        </div>
        <div class="question-content">

            <div class="right-content" v-if="$props.question.questionList[0].id == 26">
                <template class="single-content" v-for="(item) in $props.question.questionList">
                    <span v-html="item.content.split('____')[0]"></span>
                    <el-input v-model="item.userAnswer" style="width: 120px;border: none;" @focus="focus(item.id)"
                        placeholder="" @input="writeAnswer" />
                    <span v-html="item.content.split('____')[1]"></span>
                </template>
            </div>
            <div class="right-content" v-else>
                <div class="single-content-div" v-for="(item, index) in $props.question.questionList">
                    <div class="single-content-div-box">
                        <div v-html="item.content.split('____')[0]"></div>
                        <el-input v-model="item.userAnswer" style="width: 120px;border: none;" @focus="focus(item.id)"
                            placeholder="" @input="writeAnswer" />
                        <div v-html="item.content.split('____')[1].split('<br/>↓')[0]"></div>
                    </div>
                    <div v-if="index != $props.question.questionList.length - 1">↓</div>
                </div>
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.el-input {
    display: inline-block;
}

.question {

    .top-box {
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-bottom: 3px solid #F7F7F7;


        .audio-box {
            display: flex;
            align-items: center;
            gap: 20px;
        }

        .progress {
            margin-bottom: 0;
            width: 340px;
        }
    }

    .title {
        font-size: 24px;
        color: #141623;
        line-height: 22px;
        margin-bottom: 15px;

        &::before {
            content: "";
            display: inline-block;
            width: 6px;
            height: 14px;
            border-radius: 3px;
            background-color: #00CCCF;
            margin-right: 12px;
            margin-top: 2px;
        }
    }

    .describe {
        font-size: 24px;
        color: #14161E;
        line-height: 30px;
        padding-bottom: 30px;
        font-weight: 400;
    }

    .question-content {
        display: flex;
        justify-content: space-between;
        padding-top: 20px;

        .right-content {
            width: 100%;
            text-align: justify;
            font-weight: 400;
        }

        .single-content {
            display: inline-block;
            margin-bottom: 20px;

            div {
                margin-right: 20px;
            }
        }

        .single-content-div {
            .single-content-div-box {
                display: flex;
                margin-bottom: 5px;
                align-items: center;
                flex-wrap: wrap;
                font-weight: 400;

                div {
                    margin-right: 10px;
                }
            }

        }
    }
}

@media (max-width: 768px) {
    .question {
        position: relative;

        .title {
            font-size: 14px;
            color: #141623;
            line-height: 18px;
        }

        .title::before {
            content: none;
        }

        .describe {
            font-weight: 400;
            font-size: 14px;
            color: #9395A4;
            line-height: 18px;
            border-bottom: none;
            padding-bottom: 0;
        }

        .question-title {
            font-weight: 500;
            font-size: 16px;
            color: #141623;
            line-height: 24px;
        }

        .audio-box {
            position: absolute;
            right: 0;
            top: 0;
        }

    }
}
</style>
